package com.xhh.nphm.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.xhh.nphm.common.utils.http.HttpClientTools;
import com.xhh.nphm.system.vo.AiAnsweringVO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @description: 百度相关接口
 * @author: sun
 * @date: 2023-11-20 16:54
 * @version 1.0
 */
@Component
public class BaiduApiUtils {

    @Value("${baidu.address}")
    private String addressUrl;//地址查询

    @Value("${baidu.keys}")
    private String hotWord;//热词

    @Value("${baidu.transact}")
    private String handleDept;//办理部门

    @Value("${baidu.type}")
    private String types;//内容类别

    @Value("${baidu.other}")
    private String otherUrl;//其他

    @Value("${baidu.aiAnswering}")
    private String aiAnswering;

    /**
     * post请求
     * @param body
     * @param url
     * @return
     */
    private String  postRequest(String body,String url){
        String result = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            // HttpGet httpGet = new HttpGet(url);
            //添加header参数
            HttpPost httpPost = new HttpPost(url);
            //这里需要指定请求参数的编码，否则默认是ISO_8859_1
            StringEntity stringEntity = new StringEntity(body,"UTF-8");
            stringEntity.setContentType("application/json");
            httpPost.setEntity(stringEntity);
            HttpResponse response = httpclient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,"UTF-8");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 纠错
     * @param parameter 请求参数
     * @return
     */
    public String errorCorrection(String parameter){
        String url = otherUrl + "/nlp-itec/poc/value_corrector";
        return postRequest(parameter, url);
    }

    /**
     * 类案推荐
     * @param parameter 请求参数
     * @return
     */
    public String caseRecommend(String parameter){
        String url = otherUrl + "/hongkou/casesim";
        return postRequest(parameter, url);
    }

    /**
     * 关键词提取
     * @param parameter 请求参数
     * @return
     */
    public String getHotWord(String parameter){
        String url = hotWord + "/hotword/v2/letterHot";
        return postRequest(parameter, url);
    }

    /**
     * 承办单位内荐
     * @param parameter 请求参数
     * @return
     */
    public String handleDeptRecommend(String parameter){
        String url = handleDept + "/hongkou/casesim";
        return postRequest(parameter, url);
    }


    /**
     * 内容类别推荐
     * @param parameter 请求参数
     * @return
     */
    public String contentTypeRecommend(String parameter){
        String url = types + "/v2/req";
        return postRequest(parameter, url);
    }

    /**
     * 地址查询
     * @param parameter
     * @return
     */
    public String addressSearch(String parameter){
        HttpGet httpGet = new HttpGet(addressUrl + "/search/v1/search?ApiAuthorization=USER_AK"+parameter);
        CloseableHttpResponse response = null;
        HttpEntity entity;
        try {
            response = HttpClients.createDefault().execute(httpGet);
            if(response.getStatusLine().getStatusCode() != 200){
                return "";
            }
            entity = response.getEntity();
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
        }
    }

    /**
     * 智能问答
     * @param params 参数实体
     * @return
     */
    public String questions(AiAnsweringVO params) {
        String url = aiAnswering + "/ghwd/web/qaSearchNationalSimple";
        String paramsToJSON = JSONObject.toJSONString(params);
        Map<String,Object> paramsToMap = JSONObject.parseObject(paramsToJSON, Map.class);
        String res = null;
        try {
            res = HttpClientTools.doPostSSL(url, paramsToMap);
            return res;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取位置信息
     * @param params 查询参数
     * @return
     */
    public String getPositionByName(String params){
        String url = aiAnswering + "/ghwd/web/getPosition";
        Map<String,Object> paramsToMap = JSONObject.parseObject(params, Map.class);
        String res = null;
        try {
            res = HttpClientTools.doPostSSL(url, paramsToMap);
            return res;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
